package com.ck567.springbootnettyprotobufjava.codec;

import com.ck567.springbootnettyprotobufjava.message.Message;
import com.ck567.springbootnettyprotobufjava.message.entity.*;
import com.ck567.springbootnettyprotobufjava.utils.OptionType;
import org.springframework.stereotype.Component;

import java.util.Arrays;

/**
 * @description: 消息编解码工厂
 */
@Component
public class MessageCodecFactory {

    /**
     * @param type    操作类型
     * @param message 消息
     * @return message
     * @description: 将消息解码成Message
     */
    public Message decodeMessage(OptionType type, byte[] message) throws Exception {

        Message msg = new Message();
        System.out.println("收到了消息："+ type.toString());
        switch (type) {
            case LoginReq:
                return msg.setType(type.getType())
                        .setMsg(LoginProto.LoginReq.getDefaultInstance().getParserForType().parseFrom(message));

        }
        throw new RuntimeException(String.format("[message]:%s,未找到该信息所对应的消息类型", Arrays.toString(message)));
    }

}
